package com.yaolong.api.modules.gen.controller;

import com.baomidou.mybatisplus.generator.IDatabaseQuery;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.yaolong.commons.response.ResponseResult;
import com.yaolong.api.modules.gen.constants.GenConstants;
import com.yaolong.api.modules.gen.model.dto.GenCodePreviewDto;
import com.yaolong.api.modules.gen.service.IGenCodeService;
import com.yaolong.api.modules.gen.utils.MapperUtils;
import com.yaolong.api.modules.gen.vo.CodeGenConfigVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yaolong
 * @version V1.0.0
 * @program EasyCode
 * @description
 * @create 2022-12-28 14:42
 **/
@RestController
@RequestMapping("/api/gen")
@Api(tags = "代码生成")
public class GenCodeController {

    @Autowired
    IGenCodeService genCodeService;


    @PostMapping("/generate/inner")
    @ApiOperation(value = "代码生成-内部文件",notes = "代码生成-内部文件")
    public ResponseResult gen(@RequestBody CodeGenConfigVo genConfig) {
        genConfig.getGenBaseConfigEntity().setGenMode(GenConstants.GEN_MODE_INNER);
        return ResponseResult.success(genCodeService.genCode(genConfig));
    }

    @PostMapping("/generate/zip")
    @ApiOperation(value = "代码生成-zip下载",notes = "代码生成-zip下载")
    public void genZip(@RequestBody CodeGenConfigVo genConfig) {
        genConfig.getGenBaseConfigEntity().setGenMode(GenConstants.GEN_MODE_ZIP);
        genCodeService.genCode(genConfig);
    }



    @PostMapping("/generate/preview")
    @ApiOperation(value = "代码生成预览",notes = "代码生成预览")
    public ResponseResult preview(@RequestBody CodeGenConfigVo genConfig) {
        return ResponseResult.success(genCodeService.previewGenCode(genConfig));
    }

    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/agilefastfk?allowMultiQueries=true&useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true";
        String username = "root";
        String password = "yin7372175240000";
        //数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig.Builder(url,username,password)
                .dbQuery(new MySqlQuery())
                .build();
        StrategyConfig strategyConfig = new StrategyConfig.Builder().enableSchema().build();

        ConfigBuilder configBuilder = new ConfigBuilder(new PackageConfig.Builder().build(),dataSourceConfig,strategyConfig,new TemplateConfig.Builder().build(),new GlobalConfig.Builder().build(),new InjectionConfig.Builder().build());
        IDatabaseQuery.DefaultDatabaseQuery defaultDatabaseQuery = new IDatabaseQuery.DefaultDatabaseQuery(configBuilder);
        List<TableInfo> tableInfos = defaultDatabaseQuery.queryTables();
        tableInfos.forEach(tableInfo -> {
            try {
                System.out.println(MapperUtils.obj2json(tableInfo));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

}
